package cn.dnui_xcz826.dnuifood_16110100826.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cn.dnui_xcz826.dnuifood_16110100826.R;
import cn.dnui_xcz826.dnuifood_16110100826.bean.CommentBean;
import cn.dnui_xcz826.dnuifood_16110100826.bean.ResultBean;
import cn.dnui_xcz826.dnuifood_16110100826.controller.MyCommentActivity;
import cn.dnui_xcz826.dnuifood_16110100826.controller.MyOrderActivity;
import cn.dnui_xcz826.dnuifood_16110100826.listener.onRetrofitListener;
import cn.dnui_xcz826.dnuifood_16110100826.model.CommentModel;

public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.ViewHolder> implements onRetrofitListener<ResultBean> {
    private CommentModel commentModel;
    private Context context;
    private List<CommentBean> data;
    private LayoutInflater layoutInflater;

    public OrderAdapter(Context context, List data, int layoutResource) {
        this.context=context;
        this.data=data;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View v = layoutInflater.from(viewGroup.getContext()).inflate(R.layout.order_item, viewGroup, false);
        return new OrderAdapter.ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(OrderAdapter.ViewHolder holder, int position) {
        final CommentBean entity = (CommentBean) data.get(position);
        if (entity==null)
            return;
        ViewHolder viewholder = (ViewHolder) holder;
        viewholder.myorder_orderid.setText("订单id："+entity.getOrder_id()+"");
        viewholder.myorder_shopname.setText(entity.getShopname());
        viewholder.myorder_ordertime.setText(entity.getOrder_time());
        viewholder.myorder_foodname.setText(entity.getFoodname());
        viewholder.myorder_sum.setText("￥" + entity.getSum());
        if(entity.getContent()!=null&&!entity.getContent().equals("")){
            viewholder.myorder_comment.setVisibility(View.GONE);
        }
        viewholder.myorder_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final EditText et = new EditText(context);
                new AlertDialog.Builder(context).setTitle("评论")
                        .setView(et)
                        .setPositiveButton("确定",new DialogInterface.OnClickListener(){
                            public void onClick(DialogInterface dialog, int which) {
                                doComment(entity.getItem_id()+"",et.getText().toString());
                            }
                        }).setNegativeButton("取消",null)
                        .show();
            }
        });

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView myorder_shopname, myorder_ordertime, myorder_foodname, myorder_sum,
                myorder_orderid;
        Button myorder_comment;
        public ViewHolder(View itemView) {
            super(itemView);
            myorder_orderid = (TextView) itemView.findViewById(R.id.myorder_orderid);
            myorder_shopname = (TextView) itemView.findViewById(R.id.myorder_shopname);
            myorder_ordertime = (TextView) itemView.findViewById(R.id.myorder_ordertime);
            myorder_foodname = (TextView) itemView.findViewById(R.id.myorder_foodname);
            myorder_sum = (TextView) itemView.findViewById(R.id.myorder_sum);
            myorder_comment = (Button) itemView.findViewById(R.id.myorder_comment);
        }
    }
    private void doComment(String item_id, String et){
        commentModel=new CommentModel();
        commentModel.insertComment(item_id,et,this);
    }
    @Override
    public void onSuccess(ResultBean result, int flag) {
        if(result.getSuccess().equals("1")){
            Intent intent=new Intent(context,MyCommentActivity.class);
            context.startActivity(intent);
            MyOrderActivity.myOrderActivity.finish();
            Toast.makeText(context,"评论成功", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(context,"评论失败", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(context,"网络错误", Toast.LENGTH_SHORT).show();
    }


}

